package module.admin;

import bean.*;
import dao.impl.*;
import module.ahleteView.AthleteIndex;
import service.EntryFormService;
import sun.swing.table.DefaultTableCellHeaderRenderer;
import util.ExcelDataUtil;
import util.actionListener.MouseEnterAction;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.Vector;

/**
 * @ Author leiHua
 * @ Date 2022/5/25 22:23
 * 树->选择结点->获取 当前AdminContent类对应的Jpenal内容
 * 采用单例模式
 */
public class AdminContent extends JPanel {
    public static int height = AdminView.jframeHeight - AdminHead.height - 100;
    public static int width = 900;
    public static JPanel competitionPanel = null;  //比赛管理结点
    public static JPanel initJpanel = null;   // 初始化结点
    public static JPanel checkJpanel = null; //审批结点
    public static JPanel writePanel = null;  // 导入运动员信息
    public static JPanel athletePanel = null; //  运动员信息
    public static JPanel updateUrlPanel = null;  //首页URL路径
    boolean allSelectFlag = false;  //是否点了全选按钮
    static JButton refresh = new JButton("刷新");  //更新数据

    static ArrayList<EntryForm> entryForms;  //需要审批的报名信息
    static EntryFormService entryFormService = new EntryFormService();  //service层

    static JTable jTable;  //excle数据表

    //刷新事件触发：  移除AdminView中的 contentLayered 中对应的一个Jpanel  再重新加载
    static void refresh(String jieDianName) {
        AdminContent adminContent = new AdminContent();
        if (jieDianName.equals("报名审批")) {
            AdminView.contentLayered.remove(checkJpanel);
            checkJpanel = null;
            checkJpanel = adminContent.getCheckPanel();
            AdminView.contentLayered.add(checkJpanel, new Integer(1));
        } else if (jieDianName.equals("比赛管理")) {
            AdminView.contentLayered.remove(competitionPanel);
            competitionPanel = null;
            competitionPanel = adminContent.getCompetitionPanel();
            AdminView.contentLayered.add(competitionPanel, new Integer(1));
        }else if (jieDianName.equals("运动员信息")) {
            AdminView.contentLayered.remove(athletePanel);
            athletePanel=null;
            athletePanel=adminContent.getAthletePanel();
            AdminView.contentLayered.add(athletePanel, new Integer(1));



        }
    }

    //thisJpanel->content->
    public AdminContent() {
        entryForms = entryFormService.selectCheck();  //查找需要审批的报名信息
        refresh.setFont(new Font("宋体", Font.TYPE1_FONT, 24));


    }

    /**
     * 一登录显示的页面
     *
     * @return
     */
    public JPanel getinitContent() {
        if (initJpanel == null) {
            initJpanel = new JPanel();
        }
        return initJpanel;
    }

    /**
     * 对应树结点的  比赛管理
     * 采用单例模式
     * return : Jpanel  比赛管理信息的容器
     */
    public JPanel getCompetitionPanel() {
        if (competitionPanel == null) {
            //创建的表格宽度
            int tableWidth = 900;
            //创建的表格高度
            int tableHeight = 650;
            JTable jTable;
            JButton add;
            JPanel jPanel = new JPanel();  //最大容器
            jPanel.setLayout(null);

            JScrollPane jScrollPane = new JScrollPane(); //滚动窗口
            jScrollPane.setSize(tableWidth, tableHeight);
            jScrollPane.setBounds(0, 0, tableWidth, tableHeight);

            jTable = new JTable(0, 5);
            jTable.setFont(new Font("微软雅黑", Font.TYPE1_FONT, 18));  //设置表格字体
            //设置行高
            jTable.setRowHeight(30);
            //设置列名
            Object[] header = {"id", "比赛名称", "比赛地点", "最多参赛人数", "比赛时间"};
            DefaultTableModel model = (DefaultTableModel) jTable.getModel();
            model.setColumnIdentifiers(header); //设置列标识符 （列名）
            //列名字体设置
            jTable.getTableHeader().setFont(new Font("宋体", Font.TYPE1_FONT, 24));
            jTable.getTableHeader().setBackground(new Color(211, 237, 215));

//-----------------------------------------------------------------------------------------
            //查询competiton表中信息
            CompetitionDaoImpl competitionDao = new CompetitionDaoImpl();
            ArrayList<Competition> arrayList = competitionDao.selectAll();
            for (Competition competition : arrayList) {
                String c_id = competition.getC_id();
                String competitionName = competition.getCompetitionName();
                String palce = competition.getPalce();
                int number = competition.getNumber();
                String time = competition.getTime();

                Vector vector = new Vector();
                vector.add(c_id);
                vector.add(competitionName);
                vector.add(palce);
                vector.add(number);
                vector.add(time);
                model.addRow(vector);
            }

            //设置文字居中显示(这段是网上copy找的)
            DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
            dc.setHorizontalAlignment(JLabel.CENTER);
            jTable.setDefaultRenderer(Object.class, dc);
            //设置表头文字居中
            DefaultTableCellHeaderRenderer hr = new DefaultTableCellHeaderRenderer();
            hr.setHorizontalAlignment(JLabel.CENTER);
            jTable.getTableHeader().setDefaultRenderer(hr);

            jTable.setModel(model);
            //将视图设为表格setViewportView
            //jScrollPane.add(jTable); (不行)
            jScrollPane.setViewportView(jTable);
            jPanel.add(jScrollPane);
            jScrollPane.setAutoscrolls(true);
            Font font = new Font("宋体", Font.BOLD, 24);
            //添加按钮
            add = new JButton("添加");
            add.setFont(font);
            add.setBounds(100, 650, 100, 40);
            jPanel.add(add);
            //修改按钮
            JButton update = new JButton("修改");
            update.setFont(font);
            update.setBounds(250, 650, 100, 40);
            jPanel.add(update);
            //查询按钮
            JButton select = new JButton("查询");
            select.setFont(font);
            select.setBounds(400, 650, 100, 40);
            JTextField fieldName = new JTextField(30);
            fieldName.setFont(font);
            fieldName.setBounds(500, 650, 150, 40);
            jPanel.add(fieldName);
            jPanel.add(select);

            //删除按钮
            JButton del = new JButton("删除");
            del.setFont(font);
            del.setBounds(650, 650, 100, 40);
            jPanel.add(del);
            //添加按钮的点击事件    1、新建窗口  2、获取填入的信息  3、写入数据库 4、给表格中添加一条数据
            add.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    JFrame jFrame = new JFrame("运动会报名");
                    Dialog dialog = new Dialog(jFrame, true);
                    jFrame.setBounds((Constant.SCREEN_WIDETH - 600) / 2, (Constant.SCREEN_HEIGHT - 400) / 2, 800, 600);
                    jFrame.setVisible(true);
                    jFrame.setLayout(null);
                    //---------------------------------------界面部分------------------------------------------
                    Font font = new Font("宋体", Font.BOLD, 24);
                    JLabel labelComName = new JLabel("比赛名称:");
                    JLabel labelPlace = new JLabel("比赛地点:");
                    JLabel labelNumber = new JLabel("最多参数人员:");
                    JLabel labelTime = new JLabel("比赛时间:");
                    labelPlace.setFont(font);
                    labelComName.setFont(font);
                    labelTime.setFont(font);
                    labelNumber.setFont(font);
                    labelComName.setBounds(150, 100, 600, 30);
                    labelPlace.setBounds(150, 150, 200, 30);
                    labelNumber.setBounds(150, 200, 200, 30);
                    labelTime.setBounds(150, 250, 200, 30);
                    JTextField jTextField1 = new JTextField(18);
                    JTextField jTextField2 = new JTextField(18);
                    JTextField jTextField3 = new JTextField(18);
                    JTextField jTextField4 = new JTextField(18);
                    jTextField1.setBounds(300, 100, 200, 40);
                    jTextField2.setBounds(300, 150, 200, 40);
                    jTextField3.setBounds(300, 200, 200, 40);
                    //jTextField4.setBounds(300, 250, 200, 40);
                    jTextField1.setFont(font);
                    jTextField2.setFont(font);
                    jTextField3.setFont(font);
                    jTextField4.setFont(font);
                    jFrame.add(labelComName);
                    jFrame.add(jTextField1);
                    jFrame.add(labelPlace);
                    jFrame.add(jTextField2);
                    jFrame.add(labelNumber);
                    jFrame.add(jTextField3);
                    jFrame.add(labelTime);
                    jFrame.add(jTextField4);
                    //年份选则框
                    String[] yearStr = new String[30];
                    yearStr[0] = "年份";
                    for (int i = 1; i < 30; i++) {
                        yearStr[i] = String.valueOf(i + 2021);
                    }
                    JComboBox<String> yearBox = new JComboBox<>(yearStr);
                    yearBox.setBounds(300, 250, 100, 40);
                    yearBox.setFont(new Font("宋体", Font.TYPE1_FONT, 20));
                    jFrame.add(yearBox);

                    //选择月份：
                    String[] monthStr = new String[13];
                    monthStr[0] = "月份";
                    for (int i = 1; i < 13; i++) {
                        monthStr[i] = String.valueOf(i);
                    }
                    JComboBox<String> monthBox = new JComboBox<>(monthStr);
                    monthBox.setBounds(400, 250, 100, 40);
                    monthBox.setFont(new Font("宋体", Font.TYPE1_FONT, 20));
                    jFrame.add(monthBox);

                    //选择日期
                    String[] dayStr = new String[32];
                    dayStr[0] = "日期";
                    for (int i = 1; i < 32; i++) {
                        dayStr[i] = String.valueOf(i);
                    }
                    JComboBox<String> dayBox = new JComboBox<>(dayStr);
                    dayBox.setBounds(500, 250, 100, 40);
                    dayBox.setFont(new Font("宋体", Font.TYPE1_FONT, 20));
                    jFrame.add(dayBox);


                    JButton certain = new JButton("确定");
                    jFrame.add(certain);
                    certain.setBounds(350, 350, 100, 60);
                    certain.setFont(font);
                    // 点击确认时写入信息
                    certain.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            String year = yearBox.getSelectedItem().toString();
                            String month = monthBox.getSelectedItem().toString();
                            String day = dayBox.getSelectedItem().toString();
                            try {
                                String text1 = jTextField1.getText();
                                String text2 = jTextField2.getText();
                                Integer text3 = Integer.valueOf(jTextField3.getText());
                                //String text4 = jTextField4.getText();
                                String data = year + "-" + month + "-" + day;
                                if (text1 != null && text2 != null) {  //内容框都不为空时
                                    String s = UUID.randomUUID().toString();
                                    Competition competition = new Competition(s, text1, text2, text3, data);
                                    competitionDao.add(competition);  //加入数据库
                                    //更新界面数据
                                    Vector vector = new Vector();
                                    vector.add(s);
                                    vector.add(text1);
                                    vector.add(text2);
                                    vector.add(text3);
                                    vector.add(data);
                                    model.addRow(vector);
                                }
                                jFrame.setVisible(false);

                            } catch (NumberFormatException ex) {
                                JLabel label = new JLabel("参赛人员框只能输入数字");
                                label.setFont(new Font("宋体", Font.BOLD, 30));
                                JOptionPane.showMessageDialog(null, label, "最多参赛人员框出错", JOptionPane.ERROR_MESSAGE);
                                ex.printStackTrace();
                            }
                        }
                    });
                }

            });

            //更新按钮事件    1、用户修改了某行并且点击了修改后  2、获取选择行  3、获取更改后的信息  4、修改数据库
            update.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int selectedRow = jTable.getSelectedRow();
                    if (selectedRow == -1) {   //没有选择项目 提示用户进行选择
                        JLabel jLabel = new JLabel("请选择项目");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "错误", JOptionPane.ERROR_MESSAGE);
                    } else {
                        int[] selectedRows = jTable.getSelectedRows();
                        for (int i = 0; i < selectedRows.length; i++) {
                            int row = selectedRows[i];
                            Object id = jTable.getValueAt(row, 0);
                            Object name = jTable.getValueAt(row, 1);
                            Object place = jTable.getValueAt(row, 2);
                            Object number = jTable.getValueAt(row, 3);
                            Object time = jTable.getValueAt(row, 4);
                            String s = number.toString();
                            Integer n = Integer.valueOf(s);
                            Competition competition = new Competition(id.toString(), name.toString(), place.toString(), n, time.toString());
                            competitionDao.update(competition);
                        }
                        JLabel jLabel = new JLabel("修改成功");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "修改", JOptionPane.OK_OPTION);

                    }
                }
            });

            //查询按钮事件
            select.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = fieldName.getText();   //获取填入的项目名
                    int rowCount = jTable.getRowCount();
                    boolean flag = false;
                    for (int i = 0; i < rowCount; i++) {
                        Object valueAt = jTable.getValueAt(i, 1);
                        String competitionName = valueAt.toString();
                        if (competitionName.equals(text)) {
                            jTable.setSelectionBackground(new Color(235, 142, 122));
                            jTable.setSelectionForeground(new Color(255, 255, 255));
                            jTable.setRowSelectionInterval(i, i);
                            flag = true;
                            break;
                        }

                    }
                    if (!flag) {
                        JLabel jLabel = new JLabel("未查找到" + text);
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            });

            //删除按钮
            del.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int selectedRow = jTable.getSelectedRow();
                    if (selectedRow == -1) {   //没有选择项目 提示用户进行选择
                        JLabel jLabel = new JLabel("请选择项目");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "错误", JOptionPane.ERROR_MESSAGE);
                    } else {
                        JLabel jLabel = new JLabel("确定删除吗");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        int k = JOptionPane.showConfirmDialog(null, jLabel, "确认框", JOptionPane.INFORMATION_MESSAGE);
                        if (k == 0) {
                            int[] selectedRows = jTable.getSelectedRows();
                            for (int i = 0; i < selectedRows.length; i++) {
                                int row = selectedRows[i];
                                Object valueAt = jTable.getValueAt(row, 0);
                                String id = valueAt.toString();
                                model.removeRow(row);
                                competitionDao.delete(id);
                            }
                        }

                    }
                }
            });
            //按钮的交反馈
            add.addMouseListener(new MouseEnterAction(add));
            del.addMouseListener(new MouseEnterAction(del));
            update.addMouseListener(new MouseEnterAction(update));
            select.addMouseListener(new MouseEnterAction(select));
            competitionPanel = jPanel;
            competitionPanel.setBounds(400, AdminHead.height, width, height);
        }
        return competitionPanel;
    }

    /**
     * 对应结点  报名审批
     *
     * @return
     */
    public JPanel getCheckPanel() {
        if (checkJpanel == null) {
            //创建的表格宽度
            int tableWidth = 900;
            //创建的表格高度
            int tableHeight = 550;
            JTable jTable;
            JButton add;
            JPanel jPanel = new JPanel();  //最大容器
            jPanel.setLayout(null);
            JScrollPane jScrollPane = new JScrollPane(); //滚动窗口
            jScrollPane.setSize(tableWidth, tableHeight);
            jScrollPane.setBounds(0, 80, tableWidth, tableHeight);
            jTable = new JTable(0, 6);
            jTable.setFont(new Font("微软雅黑", Font.TYPE1_FONT, 18));  //设置表格字体
            //设置行高
            jTable.setRowHeight(30);
            //设置列名
            Object[] header = {"运动员姓名", "比赛名称", "比赛地点", "比赛时间", "状态", "e_id"};
            DefaultTableModel model = (DefaultTableModel) jTable.getModel();
            model.setColumnIdentifiers(header); //设置列标识符 （列名）
            //列名字体设置
            jTable.getTableHeader().setFont(new Font("宋体", Font.TYPE1_FONT, 24));
            jTable.getTableHeader().setBackground(new Color(211, 237, 215));
//-----------------------------------------------------------------------------------------
            //查询报名表需要审批的信息

            for (EntryForm entryForm : entryForms) {
                String e_id = entryForm.getId();
                Athlete athlete = entryForm.getAthlete();
                Competition competition = entryForm.getCompetition();
                String status = entryForm.getStatus();
                String name = athlete.getName();
                String competitionName = competition.getCompetitionName();
                String palce = competition.getPalce();
                String time = competition.getTime();
                Vector vector = new Vector();
                vector.add(name);
                vector.add(competitionName);
                vector.add(palce);
                vector.add(time);
                vector.add(status);
                vector.add(e_id);
                model.addRow(vector);
            }
            //设置文字居中显示(网上copy)
            DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
            dc.setHorizontalAlignment(JLabel.CENTER);
            jTable.setDefaultRenderer(Object.class, dc);
            //设置表头文字居中
            DefaultTableCellHeaderRenderer hr = new DefaultTableCellHeaderRenderer();
            hr.setHorizontalAlignment(JLabel.CENTER);
            jTable.getTableHeader().setDefaultRenderer(hr);
            jTable.setModel(model);
            jScrollPane.setViewportView(jTable);
            jPanel.add(jScrollPane);
            jScrollPane.setAutoscrolls(true);

            JButton pass = new JButton("审批通过");
            pass.setFont(new Font("宋体", Font.TYPE1_FONT, 24));
            pass.addMouseListener(new MouseEnterAction(pass));
            pass.setBounds(380, 20, 150, 40);
            JPanel jPanel1 = new JPanel();
            jPanel1.setBounds(0, 0, width, 80);
            jPanel1.setLayout(null);
            jPanel1.add(pass);
            jPanel1.setBackground(new Color(130, 144, 145));
            JButton selectAll = new JButton("全选");
            selectAll.setFont(new Font("宋体", Font.TYPE1_FONT, 24));
            selectAll.setBounds(600, 20, 80, 40);
            jPanel1.add(selectAll);
            refresh.setBounds(700, 20, 80, 40);
            jPanel1.add(refresh);
            jPanel.add(jPanel1);
            //审批通过按钮
            pass.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int selectedRow = jTable.getSelectedRow();
                    if (selectedRow == -1) {   //没有选择项目 提示用户进行选择
                        JLabel jLabel = new JLabel("未选择");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "请选择", JOptionPane.ERROR_MESSAGE);
                    } else {
                        JLabel jLabel = new JLabel("确认通过吗");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        int k = JOptionPane.showConfirmDialog(null, jLabel, "确认框", JOptionPane.INFORMATION_MESSAGE);
                        if (k == 0) {
                            EntryFormDaoImpl entryFromDao = new EntryFormDaoImpl();
                            int[] selectedRows = jTable.getSelectedRows();
                            jTable.clearSelection();
                            for (int i = 0; i < selectedRows.length; i++) {
                                int row = selectedRows[i];
                                Object valueAt = jTable.getValueAt(row, 5);
                                String id = valueAt.toString();
                                EntryForm entryForm1 = entryFromDao.select(id);
                                entryForm1.setStatus(Constant.STATUS_Ok);
                                entryFromDao.update(entryForm1);
//                                model.removeRow(row);
                            }
                            //更新表格视图界面
                            model.setRowCount(0);
                            entryForms = entryFormService.selectCheck();
                            for (EntryForm entryForm : entryForms) {
                                String e_id = entryForm.getId();
                                Athlete athlete = entryForm.getAthlete();
                                Competition competition = entryForm.getCompetition();
                                String status = entryForm.getStatus();
                                String name = athlete.getName();
                                String competitionName = competition.getCompetitionName();
                                String palce = competition.getPalce();
                                String time = competition.getTime();
                                Vector vector = new Vector();
                                vector.add(name);
                                vector.add(competitionName);
                                vector.add(palce);
                                vector.add(time);
                                vector.add(status);
                                vector.add(e_id);
                                model.addRow(vector);
                            }
                            //修改全选按钮颜色
                            allSelectFlag=false;
                            selectAll.setBackground(null);
                        }
                    }
                }
            });

            //全选按钮
            selectAll.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (!allSelectFlag) {
                        jTable.selectAll();
                        selectAll.setBackground(Color.red);
                        allSelectFlag = true;
                    } else {
                        jTable.clearSelection();
                        selectAll.setBackground(null);
                        allSelectFlag = false;
                    }


                }
            });
            //刷新按钮
            refresh.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    AdminContent.refresh("报名审批");
                }
            });
            checkJpanel = jPanel;
            checkJpanel.setBounds(400, AdminHead.height, width, height);
            return checkJpanel;
        } else {
            return checkJpanel;
        }
    }

    /**
     * 对应结点 ：批量导入运动员信息
     *
     * @return
     */
    public JPanel getWritePanel() {
        if (AdminContent.writePanel == null) {
            JPanel jPanel1 = new JPanel();  //最外层容器    jpanel1-> jpanel（滚动框（表格））+按钮
            jPanel1.setLayout(null);
            Font font = new Font("宋体", Font.TYPE1_FONT, 24);
            JLabel label = new JLabel("请输入excel文件的路径:");
            label.setBounds(50, 20, 350, 40);
            label.setFont(font);
            jPanel1.add(label);
            JTextField path = new JTextField(40);
            path.setBounds(360, 20, 350, 40);
            path.setFont(font);
            jPanel1.add(path);
            JButton open = new JButton("打开");
            open.setFont(font);
            open.setBounds(700, 20, 80, 40);
            open.addMouseListener(new MouseEnterAction(open));
            JLabel label1 = new JLabel("导入excel文件列名顺序必须是：用户名,密码，性别，年龄，系别;文件路径需要包含后缀");
            label1.setFont(new Font("宋体", Font.TYPE1_FONT, 15));
            label1.setBounds(50, 65, 900, 20);
            jPanel1.add(label1);
            JButton write = new JButton("写入数据库");
            write.setFont(new Font("宋体", Font.TYPE1_FONT, 24));
            write.setBounds(400, 650, 200, 40);
            write.addMouseListener(new MouseEnterAction(write));
            jPanel1.add(write);

            //打开文件按钮
            open.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        String pathText = path.getText();
                        if (jTable != null) {
                            jTable = null;
                        }
                        JPanel jPanel = new JPanel();  //表格容器
                        int tableWidth = 850;
                        //创建的表格高度
                        int tableHeight = 550;
                        JScrollPane jScrollPane = new JScrollPane();
                        jScrollPane.setSize(tableWidth, tableHeight);
                        jScrollPane.setBounds(0, 0, tableWidth, tableHeight);
                        jTable = new JTable(0, 5);
                        jTable.setFont(new Font("微软雅黑", Font.TYPE1_FONT, 18));  //设置表格字体
                        //设置行高
                        jTable.setRowHeight(30);
                        //设置列名
                        Object[] header = {"用户名", "密码", "性别", "年龄", "系别"};
                        DefaultTableModel model = (DefaultTableModel) jTable.getModel();

                        model.setColumnIdentifiers(header); //设置列标识符 （列名）
                        //列名字体设置
                        jTable.getTableHeader().setFont(new Font("宋体", Font.TYPE1_FONT, 24));
                        jTable.getTableHeader().setBackground(new Color(211, 237, 215));

                        //设置文字居中显示(这段是网上copy找的)
                        DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
                        dc.setHorizontalAlignment(JLabel.CENTER);
                        jTable.setDefaultRenderer(Object.class, dc);
                        //设置表头文字居中
                        DefaultTableCellHeaderRenderer hr = new DefaultTableCellHeaderRenderer();
                        hr.setHorizontalAlignment(JLabel.CENTER);

                        //读取excel文件中的数据
                        List<Map<String, String>> maps = ExcelDataUtil.redExcel(pathText);
                        for (Map<String, String> map : maps) {
                            String name = map.get("用户名");
                            String password = map.get("密码");
                            String sex = map.get("性别");
                            int age = 0;
                            boolean ageIsNull = true;  //判断age框是否是空的
                            if (map.get("年龄") != null) {
                                age = Integer.valueOf(map.get("年龄"));
                                ageIsNull = false;
                            }
                            String department = map.get("系别");
                            Vector vector = new Vector();
                            vector.add(name);
                            vector.add(password);
                            vector.add(sex);
                            if (!ageIsNull) {
                                vector.add(age);
                            } else {
                                vector.add(null);
                            }
                            vector.add(department);
                            model.addRow(vector);
                        }
                        jTable.getTableHeader().setDefaultRenderer(hr);
                        jTable.setModel(model);
                        jScrollPane.setViewportView(jTable);
                        jPanel.add(jScrollPane);
                        jPanel.setBounds(20, 80, 900, 550);

                        jPanel.remove(jScrollPane);
                        jPanel.add(jScrollPane);
                        jPanel1.remove(jPanel);
                        jPanel1.add(jPanel);

                    } catch (Exception ex) {
                        JLabel jLabel = new JLabel("打开失败");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "是否覆盖写", JOptionPane.ERROR_MESSAGE);
                        ex.printStackTrace();
                    }
                }
            });

            write.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    AthleteDaoImpl athleteDao = new AthleteDaoImpl();
                    UserDaoImpl userDao = new UserDaoImpl();
                    //查找出已有的运动员名
                    ArrayList<Athlete> athleteList = athleteDao.selectAll();
                    ArrayList<String> nameList = new ArrayList<String>();
                    for (Athlete athlete : athleteList) {
                        String name = athlete.getName();
                        nameList.add(name);
                    }
                    int rowCount = jTable.getRowCount();
                    for (int i = 0; i < rowCount; i++) {
                        String name = jTable.getValueAt(i, 0).toString();
                        String password = jTable.getValueAt(i, 1).toString();
                        String sex = jTable.getValueAt(i, 2).toString();
                        String age = jTable.getValueAt(i, 3).toString();
                        String department = jTable.getValueAt(i, 4).toString();
                        Integer intage = Integer.valueOf(age);
                        String id = UUID.randomUUID().toString();
                        Athlete athlete = new Athlete(id, name, sex, intage, department, null);  //该行的数据
                        User user = new User(id, name, password);
                        boolean overWrite = false;
                        boolean isExist = false;
                        for (String s : nameList) {
                            if (name.equals(s)) {
                                JLabel jLabel = new JLabel("第" + (i + 1) + "行" + s + "已存在,是否需要覆盖写入");
                                jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                                int k = JOptionPane.showConfirmDialog(null, jLabel, "错误", JOptionPane.ERROR_MESSAGE);
                                if (k == 0) overWrite = true;  //用户选择了确定 ，则进行覆盖写入  ，若是点击了取消 则进行写入
                                isExist = true;
                            }
                        }
                        if (overWrite) {
                            Athlete originAthlete = athleteDao.selectDependName(name, null);
                            athleteDao.update(originAthlete);
                            userDao.update(user);
                            continue;
                        }
                        if (!isExist) {  // 不存在重复name时写入
                            athleteDao.add(athlete);
                            userDao.add(user);
                        }

                    }
                    ImageIcon imageIcon = new ImageIcon("src/imag/OK.jpg");
                    imageIcon.setImage(imageIcon.getImage().getScaledInstance(50, 50, 1));
                    JLabel jLabel = new JLabel("完成");
                    jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                    JOptionPane.showMessageDialog(null, jLabel, "成功", JOptionPane.DEFAULT_OPTION, imageIcon);
                    //更新运动员信息界面
                    refresh("运动员信息");
                }
            });
            jPanel1.add(open);
            jPanel1.setBounds(400, AdminHead.height, width, height);
            writePanel = jPanel1;
            return writePanel;
        } else {
            return writePanel;
        }

    }

    /**
     * 对应结点 ：运动员信息管理
     */
    public JPanel getAthletePanel() {
        if (athletePanel == null) {
            //创建的表格宽度
            int tableWidth = 900;
            //创建的表格高度
            int tableHeight = 650;
            JTable jTable;
            JButton add;
            JPanel jPanel = new JPanel();  //最大容器
            jPanel.setLayout(null);
            JScrollPane jScrollPane = new JScrollPane(); //滚动窗口
            jScrollPane.setSize(tableWidth, tableHeight);
            jScrollPane.setBounds(0, 0, tableWidth, tableHeight);
            jTable = new JTable(0, 5);
            jTable.setFont(new Font("微软雅黑", Font.TYPE1_FONT, 18));  //设置表格字体
            //设置行高
            jTable.setRowHeight(30);
            //设置列名
            Object[] header = {"id", "用户米", "密码", "性别", "年龄", "系别"};
            DefaultTableModel model = (DefaultTableModel) jTable.getModel();
            model.setColumnIdentifiers(header); //设置列标识符 （列名）
            //列名字体设置
            jTable.getTableHeader().setFont(new Font("宋体", Font.TYPE1_FONT, 24));
            jTable.getTableHeader().setBackground(new Color(211, 237, 215));
//-----------------------------------------------------------------------------------------
            //查询信息
            AthleteDaoImpl athleteDao = new AthleteDaoImpl();
            UserDaoImpl userDao = new UserDaoImpl();
            ArrayList<Athlete> athleteArrayList = athleteDao.selectAll();
            for (Athlete athlete : athleteArrayList) {
                String a_id = athlete.getA_id();
                String name = athlete.getName();
                String sex = athlete.getSex();
                int age = athlete.getAge();
                String department = athlete.getDepartment();
                User user = userDao.select(a_id);
                String password = user.getPassword();
                Vector vector = new Vector();
                vector.add(a_id);
                vector.add(name);
                vector.add(password);
                vector.add(sex);
                vector.add(age);
                vector.add(department);
                model.addRow(vector);
            }
            //设置文字居中显示(这段是网上copy找的)
            DefaultTableCellRenderer dc = new DefaultTableCellRenderer();
            dc.setHorizontalAlignment(JLabel.CENTER);
            jTable.setDefaultRenderer(Object.class, dc);
            //设置表头文字居中
            DefaultTableCellHeaderRenderer hr = new DefaultTableCellHeaderRenderer();
            hr.setHorizontalAlignment(JLabel.CENTER);
            jTable.getTableHeader().setDefaultRenderer(hr);

            jTable.setModel(model);
            //将视图设为表格setViewportView
            //jScrollPane.add(jTable); (不行)
            jScrollPane.setViewportView(jTable);
            jPanel.add(jScrollPane);
            jScrollPane.setAutoscrolls(true);
            Font font = new Font("宋体", Font.BOLD, 24);
            //添加按钮
            add = new JButton("添加");
            add.setFont(font);
            add.setBounds(100, 650, 100, 40);
            add.addMouseListener(new MouseEnterAction(add));
            jPanel.add(add);
            //修改按钮
            JButton update = new JButton("修改");
            update.setFont(font);
            update.setBounds(250, 650, 100, 40);
            update.addMouseListener(new MouseEnterAction(update));
            jPanel.add(update);
            //查询按钮
            JButton select = new JButton("查询");
            select.setFont(font);
            select.setBounds(400, 650, 100, 40);
            select.addMouseListener(new MouseEnterAction(select));
            JTextField fieldName = new JTextField(30);
            fieldName.setFont(font);
            fieldName.setBounds(500, 650, 150, 40);
            jPanel.add(fieldName);
            jPanel.add(select);

            //删除按钮
            JButton del = new JButton("删除");
            del.setFont(font);
            del.setBounds(650, 650, 100, 40);
            del.addMouseListener(new MouseEnterAction(del));
            jPanel.add(del);
            //删除事件
            del.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int selectedRow = jTable.getSelectedRow();
                    if (selectedRow == -1) {   //没有选择项目 提示用户进行选择
                        JLabel jLabel = new JLabel("请选择项目");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "错误", JOptionPane.ERROR_MESSAGE);
                    } else {
                        JLabel jLabel = new JLabel("确定删除吗");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        int k = JOptionPane.showConfirmDialog(null, jLabel, "确认框", JOptionPane.INFORMATION_MESSAGE);
                        if (k == 0) {
                            int[] selectedRows = jTable.getSelectedRows();
                            for (int i = 0; i < selectedRows.length; i++) {
                                int row = selectedRows[i];
                                Object valueAt = jTable.getValueAt(row, 0);
                                String id = valueAt.toString();
                                model.removeRow(row);
                                userDao.delete(id);
                                athleteDao.delete(id);
                            }
                        }
                    }
                }
            });

            //查询按钮事件
            select.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String text = fieldName.getText();   //获取填入的项目名
                    int rowCount = jTable.getRowCount();
                    boolean flag = false;
                    for (int i = 0; i < rowCount; i++) {
                        Object valueAt = jTable.getValueAt(i, 1);
                        String athleteName = valueAt.toString();
                        if (athleteName.equals(text)) {
                            jTable.setSelectionBackground(new Color(235, 142, 122));
                            jTable.setSelectionForeground(new Color(255, 255, 255));
                            jTable.setRowSelectionInterval(i, i);
                            flag = true;
                            break;
                        }

                    }
                    if (!flag) {
                        JLabel jLabel = new JLabel("未查找到" + text);
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
            });

            //更新按钮事件
            update.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int selectedRow = jTable.getSelectedRow();
                    if (selectedRow == -1) {   //没有选择项目 提示用户进行选择
                        JLabel jLabel = new JLabel("请选择项目");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "错误", JOptionPane.ERROR_MESSAGE);
                    } else {
                        int[] selectedRows = jTable.getSelectedRows();
                        for (int i = 0; i < selectedRows.length; i++) {
                            int row = selectedRows[i];
                            Object id = jTable.getValueAt(row, 0);
                            Object name = jTable.getValueAt(row, 1);
                            Object password = jTable.getValueAt(row, 2);
                            Object sex = jTable.getValueAt(row, 3);
                            Object age = jTable.getValueAt(row, 4);
                            Object depatement = jTable.getValueAt(row, 5);
                            String s = age.toString();
                            Integer n = Integer.valueOf(s);
                            Athlete athlete = new Athlete(id.toString(), name.toString(), sex.toString(), n, depatement.toString(), null);
                            User user = new User(id.toString(), name.toString(), password.toString());
                            athleteDao.update(athlete);
                            userDao.update(user);
                        }
                        JLabel jLabel = new JLabel("修改成功");
                        jLabel.setFont(new Font("宋体", Font.BOLD, 30));
                        JOptionPane.showMessageDialog(null, jLabel, "修改", JOptionPane.OK_OPTION);

                    }
                }
            });

            //添加按钮事件
            add.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {

                    JFrame jFrame = new JFrame("添加用户");
                    JPanel jPanel = new JPanel();
                    jPanel.setBounds(0, 0, 800, 600);
                    jPanel.setLayout(null);
                    jPanel.setOpaque(false);
                    jPanel.setForeground(Color.white);
                    Dialog dialog = new Dialog(jFrame, true);
                    jFrame.setBounds((Constant.SCREEN_WIDETH - 600) / 2, (Constant.SCREEN_HEIGHT - 400) / 2, 800, 600);
                    jFrame.setVisible(true);
                    jFrame.setLayout(null);
                    //---------------------------------------界面部分------------------------------------------
                    JLayeredPane layeredPane = jFrame.getLayeredPane();
                    JLabel label = new JLabel();
                    ImageIcon imageIcon = new ImageIcon("src/imag/admin2.jpg");
                    imageIcon.setImage(imageIcon.getImage().getScaledInstance(800, 600, 1));
                    label.setIcon(imageIcon);
                    label.setBounds(0, 0, 800, 600);
                    layeredPane.add(label, new Integer(1));
                    layeredPane.add(jPanel, new Integer(2));
                    Font font = new Font("宋体", Font.BOLD, 24);
                    JLabel labelComName = new JLabel("用户名:");
                    JLabel labelPlace = new JLabel("密码:");
                    JLabel labelNumber = new JLabel("年龄:");
                    JLabel labelTime = new JLabel("性别:");
                    JLabel labelDepartment = new JLabel("系别:");
                    labelPlace.setFont(font);
                    labelPlace.setForeground(Color.white);
                    labelComName.setFont(font);
                    labelComName.setForeground(Color.white);
                    labelTime.setFont(font);
                    labelTime.setForeground(Color.white);
                    labelNumber.setFont(font);
                    labelNumber.setForeground(Color.white);
                    labelDepartment.setFont(font);
                    labelDepartment.setForeground(Color.white);
                    labelComName.setBounds(150, 100, 600, 30);
                    labelPlace.setBounds(150, 150, 200, 30);
                    labelNumber.setBounds(150, 200, 200, 30);
                    labelTime.setBounds(150, 250, 200, 30);
                    labelDepartment.setBounds(150, 300, 200, 30);
                    JTextField jTextField1 = new JTextField(18);
                    JTextField jTextField2 = new JTextField(18);
                    JTextField jTextField3 = new JTextField(18);
                    JTextField jTextField4 = new JTextField(18);
                    JTextField jTextField5 = new JTextField(18);
                    jTextField1.setBounds(300, 100, 200, 40);
                    jTextField2.setBounds(300, 150, 200, 40);
                    jTextField3.setBounds(300, 200, 200, 40);
                    jTextField4.setBounds(300, 250, 200, 40);
                    jTextField5.setBounds(300, 300, 200, 40);
                    jTextField1.setFont(font);
                    jTextField2.setFont(font);
                    jTextField3.setFont(font);
                    jTextField4.setFont(font);
                    jTextField5.setFont(font);
                    jPanel.add(labelComName);
                    jPanel.add(jTextField1);
                    jPanel.add(labelPlace);
                    jPanel.add(jTextField2);
                    jPanel.add(labelNumber);
                    jPanel.add(jTextField3);
                    jPanel.add(labelTime);
                    jPanel.add(jTextField4);
                    jPanel.add(labelDepartment);
                    jPanel.add(jTextField5);

                    JButton certain = new JButton("确定");
                    jPanel.add(certain);
                    certain.setBounds(350, 350, 100, 60);
                    certain.setFont(font);
                    // 点击确认时写入信息
                    certain.addActionListener(new ActionListener() {
                        @Override
                        public void actionPerformed(ActionEvent e) {

                            try {
                                String text1 = jTextField1.getText();
                                String text2 = jTextField2.getText();
                                String text3 = jTextField4.getText();
                                Integer text4 = Integer.valueOf(jTextField3.getText());
                                String text5 = jTextField5.getText();
                                if (text1 != null && text2 != null && text3 != null && text4 != null) {  //内容框都不为空时
                                    String s = UUID.randomUUID().toString();
                                    Athlete athlete = new Athlete(s, text1, text3, text4, text5, null);
                                    User user = new User(s, text1, text2);
                                    athleteDao.add(athlete); //加入数据库
                                    userDao.add(user);
                                    //更新界面数据
                                    Vector vector = new Vector();
                                    vector.add(s);
                                    vector.add(text1);
                                    vector.add(text2);
                                    vector.add(text3);
                                    vector.add(text4);
                                    vector.add(text5);
                                    model.addRow(vector);
                                }
                                jFrame.setVisible(false);

                            } catch (NumberFormatException ex) {
                                JLabel label = new JLabel("参赛人员框只能输入数字");
                                label.setFont(new Font("宋体", Font.BOLD, 30));
                                JOptionPane.showMessageDialog(null, label, "最多参赛人员框出错", JOptionPane.ERROR_MESSAGE);
                                ex.printStackTrace();
                            }
                        }
                    });
                }

            });


            athletePanel = jPanel;
            athletePanel.setBounds(400, AdminHead.height, width, height);
            return athletePanel;
        } else {
            return athletePanel;
        }


    }

    /**
     * 对应结点  更改首页URL路径
     */
    public JPanel getUpdateUrlPanel() {
        if (updateUrlPanel == null) {
            JPanel jPanel = new JPanel();
            jPanel.setLayout(null);
            FileDaoImpl fileDao = new FileDaoImpl();
            JLabel title = new JLabel("更改URL页面");
            title.setBounds(350, 20, 500, 80);
            title.setFont(new Font("宋体", Font.BOLD, 35));
            title.setForeground(Color.magenta);

            JLabel label = new JLabel("当前首页URL链接路径:" + fileDao.selectIndexUrl());
            label.setBounds(50, 150, 800, 50);

            JLabel after = new JLabel("请输入想要更改的URL:");
            after.setBounds(50, 250, 400, 50);

            JTextField textField = new JTextField(30);
            textField.setBounds(50, 300, 800, 50);
            Font font = new Font("宋体", Font.TYPE1_FONT, 24);
            label.setFont(font);
            after.setFont(font);
            textField.setFont(font);

            JButton updateButton = new JButton("修改");
            updateButton.setBounds(400, 400, 100, 50);
            updateButton.setFont(font);
            updateButton.addMouseListener(new MouseEnterAction(updateButton));
            updateButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String url = textField.getText();
                    new FileDaoImpl().updateIndexUrl(url);
                    AthleteIndex.url=url;
                    label.setText(url);
                    JLabel label = new JLabel("修改成功");
                    label.setFont(new Font("宋体", Font.BOLD, 30));
                    ImageIcon imageIcon = new ImageIcon("src/imag/OK.jpg");
                    imageIcon.setImage(imageIcon.getImage().getScaledInstance(50, 50, 1));
                    JOptionPane.showMessageDialog(null, label, "成功",JOptionPane.DEFAULT_OPTION ,imageIcon);
                }
            });
            jPanel.add(title);
            jPanel.add(label);
            jPanel.add(after);
            jPanel.add(textField);
            jPanel.add(updateButton);
            updateUrlPanel = jPanel;
            updateUrlPanel.setBounds(400, AdminHead.height, width, height);
            return updateUrlPanel;
        } else {
            return updateUrlPanel;
        }
    }
}
